﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SBACN.WebSite.Cache;
using SBACN.WebSite.Code;
using SBACN.WebSite.Models;
using SBACN.WebSite.Common;

namespace SBACN.WebSite.UC
{
	public partial class AdminMenu : AdminBaseControl
	{
		public int MenuIndex
		{
			get
			{
				if (ViewState["MenuIndex"] == null)
				{
					if (!string.IsNullOrWhiteSpace(SystemCode))
					{
						return Convert.ToInt32(SystemCode.Substring(0, 4)) - 1;
					}
				}
				else
				{
					return Convert.ToInt32(ViewState["MenuIndex"]);
				}
				return 0;
			}
			set
			{
				ViewState["MenuIndex"] = value;
			}
		}
		private string ShowCommiteeCode
		{
			get
			{
				return (SystemCode.Length >= 8 && SystemCode.StartsWith("0003")) ? SystemCode.Substring(0, 8) : "00030001";
			}
		}
		protected void Page_Load(object sender, EventArgs e)
		{
			//if(!Page.IsPostBack)
			{
				InitCtrl();
			}
		}
		private int grandChildCount = 0;
		public void InitCtrl()
		{
			var itemList = SystemMapCache.Instance.List.Where(p => p.IsAdminRight && p.Status && AdminBasePage.CheckReadRight(AdminBasePage.AdminInfo, p.AdminURL) && AdminBasePage.CheckCodeRight(AdminBasePage.AdminInfo, p.Code)).ToList();
			var fullItemList = new List<TB_SystemMap>();
			foreach(var item in itemList)
			{
				IList<TB_SystemMap> parentList = SystemMapCache.Instance.GetParent(item.Code);
				foreach(var parent in parentList)
				{
					if(!fullItemList.Contains(parent))
					{
						fullItemList.Add(parent);
					}
				} 
			}

			bool findCode = false;
			MenuIndex = 0;
			StringBuilder sb = new StringBuilder();
			foreach (var item in fullItemList.Where(p => p.Code.Length == 4).OrderBy(p => p.SortIndex).ThenBy(p => p.Code))
			{
				if(item.Code.Length == 4)
				{
					if (!findCode)
					{
						if (SystemCode.StartsWith(item.Code))
						{
							findCode = true;
						}
						else
						{
							MenuIndex++;
						}
					}
					sb.Append(CreateMenu(item, fullItemList));
				}
			}
			this.left_Div.InnerHtml = sb.ToString();
		}

		protected string CreateMenu(TB_SystemMap curMap, IList<TB_SystemMap> list)
		{
			StringBuilder sb = new StringBuilder();

			var childList = list.Where(p => p.Code.StartsWith(curMap.Code) && p.Code.Length == curMap.Code.Length + 4).OrderBy(p => p.SortIndex).ThenBy(p => p.Code);

			if (curMap.Code.Length == 4)
			{
				if(childList.Count() > 0)
				{
					sb.Append("<div class='topmenu' title='" + curMap.Name + "'>");
					sb.Append(curMap.Name.CutTo(8, 7));
					sb.Append("</div>");
					int height = 0;

					#region 专委会特殊处理

					if (curMap.Code == "0003")
					{
						//height = childList.Where(p => p.Code.Length == 8).Count() * 25;
						foreach (var child in childList.Where(p => p.Code.Length == 8).ToList())
						{
							int gcount = list.Where(p => p.Code.StartsWith(child.Code) && (p.Code.Length == child.Code.Length + 4)).Count();
							grandChildCount = Math.Max(grandChildCount, gcount);
						}
						//height += grandChildCount * 18;
					}

					#endregion

					sb.Append("<div class='leftLevel2'" + (height == 0 ? string.Empty : " style='height:" + height + "px' ") + ">");
					foreach (var item in childList)
					{
						sb.Append(CreateMenu(item, list));
					}
					sb.Append("</div>");
					
				}
			}

			if (curMap.Code.Length == 8)
			{
				if(childList.Count() > 0)
				{
					sb.Append("<div class='childMain' " + (curMap.Code.StartsWith("0003") ? "onclick='return ShowMenu(\"" + curMap.Code + "\");'" : string.Empty) + " title='" + curMap.Name + "'>");
					sb.Append(curMap.Name.CutTo(7, 6));
					sb.Append("</div>");
					sb.Append("<div class='childLevel2'");

					if (curMap.Code.StartsWith("0003") && curMap.Code.Length == 8)
					{
						var commiteeCount = list.Where(p => p.Code.StartsWith("0003") && p.Code.Length == 8).Count();
						sb.Append("style='display:" + (curMap.Code == ShowCommiteeCode || commiteeCount == 1 ? "block;" : "none;") + "' id='" + curMap.Code + "'");
					}
					sb.Append(">");
					foreach (var item in childList)
					{
						sb.Append(CreateMenu(item, list));
					}
				
					if (curMap.Code.StartsWith("0003") && curMap.Code.Length == 8)
					{
						for (int i = 0; i < grandChildCount - childList.Count(); i++)
						{
							sb.Append("<div style='height:18px;'></div>");
						}
					}
					sb.Append("</div>");
				}
				else
				{
					string url = BasePage.UrlAppendQueryString(curMap.AdminURL, "st", curMap.Code, true);

					sb.Append("<div class=\"listmenu\"><a onclick='return MenuClick(this);' title='" + curMap.Name + "' href='" + url + "' " + (curMap.OpenInNewWindow ? "target='_blank' " : string.Empty) + (curMap.Code == SystemCode && !IsEn ? " style='color:#000080;' " : "") + ">");
					sb.Append(curMap.Name.CutTo(6, 5));
					sb.Append("</a></div>");

					//!string.IsNullOrWhiteSpace(curMap.EText) || 
					if (!string.IsNullOrWhiteSpace(curMap.EURL) && curMap.Code != "00010004" && curMap.Code != "00010005")
					{
						sb.Append("<div class=\"listmenu\"><a onclick='return MenuClick(this);' title='" + curMap.Name + "(英)' href='" + BasePage.UrlAppendQueryString(url, "en", "1", true) + "' " + (curMap.OpenInNewWindow ? "target='_blank' " : string.Empty) + (curMap.Code == SystemCode && IsEn ? " style='color:#000080;' " : "") + ">");
						sb.Append(curMap.Name.CutTo(4, 3));
						sb.Append("(英)</a></div>");
					}
				}
			}

			if (curMap.Code.Length == 12)
			{
				string url = BasePage.UrlAppendQueryString(curMap.AdminURL, "st", curMap.Code, true);
				sb.Append("<div><a onclick='return MenuClick(this);' title='" + curMap.Name + "' href='" + url + "' " + (curMap.OpenInNewWindow ? "target='_blank' " : string.Empty) + (curMap.Code == SystemCode && !IsEn ? " style='color:#000080;' " : "") + ">");
				sb.Append(curMap.Name.CutTo(4, 3));
				sb.Append("</a></div>");

				//!string.IsNullOrWhiteSpace(curMap.EText) || 
				if (!string.IsNullOrWhiteSpace(curMap.EURL) && curMap.Code != "00010004" && curMap.Code != "00010005")
				{
					sb.Append("<div><a onclick='return MenuClick(this);' title='" + curMap.Name + "' href='" + BasePage.UrlAppendQueryString(url, "en", "1", true) + "' " + (curMap.OpenInNewWindow ? "target='_blank' " : string.Empty) + (curMap.Code == SystemCode && IsEn ? " style='color:#000080;' " : "") + ">");
					sb.Append(curMap.Name.CutTo(3, 2));
					sb.Append("(英)</a></div>");
				}
			}

			return sb.ToString();
		}
	}
}